package algorithm.dp;

public class 编辑距离 {
    /*
     * 给定两个单词 word1 和 word2，计算出将 word1 转换成 word2 所使用的最少操作数 。
     * 
     * 你可以对一个单词进行如下三种操作：
     * 
     * 插入一个字符 删除一个字符 替换一个字符 示例 1:
     * 
     * 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 'h' 替换为
     * 'r') rorse -> rose (删除 'r') rose -> ros (删除 'e') 示例 2:
     * 
     * 输入: word1 = "intention", word2 = "execution" 输出: 5 解释: intention ->
     * inention (删除 't') inention -> enention (将 'i' 替换为 'e') enention ->
     * exention (将 'n' 替换为 'x') exention -> exection (将 'n' 替换为 'c') exection ->
     * execution (插入 'u')
     */
    public int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        /*
         * if(word1[i]!=word2[j]) case insert: judege(i,j+1)+1 case delete:
         * judege(i+1,j)+1 case change: judege(i+1,j+1)+1
         *
         * if(word1[i] == word2[j] judge(i+1,j+1);
         */
        return dp(word1, 0, word2, 0);
    }

    int[][] record = null;

    int dp(String word1, int i, String word2, int j) {
        int len1 = word1.length();
        int len2 = word2.length();
        if (record == null) {
            record = new int[len1 + 5][len2 + 5];
            for (int m = 0; m <= len1; m++) {
                for (int n = 0; n <= len2; n++) {
                    record[m][n] = -1;
                }
            }
        }
        if (record[i][j] != -1) {
            return record[i][j];
        }
        if (i == word1.length()) {
            record[i][j] = word2.length() - j;
            return record[i][j];// insert
        }
        if (j == word2.length()) {
            record[i][j] = word1.length() - i;
            return record[i][j];// delete
        }
        if (word1.charAt(i) == word2.charAt(j)) {
            record[i][j] = dp(word1, i + 1, word2, j + 1);
        } else {
            int min = dp(word1, i, word2, j + 1);// insert
            min = Math.min(dp(word1, i + 1, word2, j), min);// delete
            min = Math.min(dp(word1, i + 1, word2, j + 1), min);// change
            record[i][j] = min + 1;
        }
        return record[i][j];
    }

    int dp2(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        int[][] dp = new int[len1 + 5][len2 + 5];
        for (int i = 0; i <= len1; i++) {
            dp[i][0] = i;
        }
        for (int j = 0; j <= len2; j++) {
            dp[0][j] = j;
        }
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;
                }
            }
        }
        return dp[len1][len2];
    }

    int min(int... nums) {
        int min = 1 << 30;
        for (int num : nums) {
            if (num != 0) {
                min = Math.min(min, num);
            }
        }
        return min == (1 << 30) ? 0 : min;
    }
}
